package com.study.d012Components;
import java.util.Iterator;
import java.util.function.Consumer;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * RadioButton,ToggleGroup
 * CheckBox
 * @author Administrator   第31课
 *
 */

public class RadioCheckBoxMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane ap = new AnchorPane();

		//单选按钮
		ToggleGroup toggle = new ToggleGroup();
		RadioButton r1 = new RadioButton("radio1");
		RadioButton r2 = new RadioButton("radio2");
		RadioButton r3 = new RadioButton("radio3");
		r1.setToggleGroup(toggle);
		r2.setToggleGroup(toggle);
		r3.setToggleGroup(toggle);

		//设置默认选中有两种方式 r2.setSelected(true) 或者toggle.selectToggle()
		//r2.setSelected(true);
		toggle.selectToggle(r2);

		//设置下划线
		r3.setUnderline(true);

		HBox box1 = new HBox();
		box1.getChildren().addAll(r1,r2,r3);

		AnchorPane.setTopAnchor(box1, 20.0);
		AnchorPane.setLeftAnchor(box1, 20.0);

		ap.getChildren().add(box1);

		stage.show();
		stage.setScene(new Scene(ap,400,300));

		//监听单选按钮组
		toggle.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
			@Override
			public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
				RadioButton r = (RadioButton)newValue;
				System.out.println("当前被选中的是"+r.getText());
			}
		});

		//监听单个RadioButton
		r1.selectedProperty().addListener(new ChangeListener<Boolean>() {
			@Override
			public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
				System.out.println("r1当前是否被选中:" + newValue);

			}
		});





		//多选按钮
		HBox box2 = new HBox();
		CheckBox b1 = new CheckBox("读书");
		CheckBox b2 = new CheckBox("写字");
		CheckBox b3 = new CheckBox("看电视");
		box2.getChildren().addAll(b1,b2,b3);

		AnchorPane.setTopAnchor(box2, 100.0);
		AnchorPane.setLeftAnchor(box2, 50.0);
		ap.getChildren().add(box2);

		//设置默认选中
		b1.setSelected(true);

		//设置不确定状态
		b2.setIndeterminate(true);

		//设置允许设置为不确定状态
		b3.setAllowIndeterminate(true);

		Button button = new Button("获取选中的复选框");

		AnchorPane.setTopAnchor(button, 140.0);
		AnchorPane.setLeftAnchor(button, 50.0);

		ap.getChildren().add(button);

		//第一种获取复选框的选中的值
/*		button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				box2.getChildren().forEach(new Consumer<Node>() {
					@Override
					public void accept(Node t) {
						CheckBox cb=(CheckBox)t;
						if(cb.isSelected()){
							System.out.println(cb.getText() + "   " );
						}
					}
				});
			}
		});*/

		//第二种获取复选框的选中的值
		/*button.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Iterator<Node> it=box2.getChildren().iterator();
				while(it.hasNext()){
					CheckBox c=(CheckBox)it.next();
					if(c.isSelected()){
						System.out.println(c.getText());
					}
				}
			}
		});*/


		//第三种获取复选框的选中的值 （lamda表达式）
		button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				box2.getChildren().forEach(item->{
					CheckBox c=(CheckBox)item;
					if(c.isSelected()){
						System.out.println(c.getText());
					}
				});
			}
		});
	}
}
